package com.health.fragment.jtys;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.zxing.common.StringUtils;
import com.health.R;
import com.health.adapter.SatisfactionAdapter;
import com.health.adapter.SuifangAdapter;
import com.health.fragment.NovaFragment;
import com.health.model.PeopleType;
import com.health.tablebean.ARCHIVE_BASEINFO;
import com.health.tablebean.CD_DIABETESFOLLOWUP;
import com.health.tablebean.CD_HYPERTENSIONFOLLOWUP;
import com.health.tablebean.CD_MENTALDISEASE_FOLLOWUP;
import com.health.tablebean.JTYSDoctorEvaluateBean;
import com.health.tablebean.SuiFangBean;
import com.health.tools.GlobalConts;
import com.health.tools.SPUtil;
import com.health.view.spinner.PieChartView;
import com.health.viewscontrols.NovaApplication;
import com.health.viewscontrols.focusgroup.GXYActivity;
import com.health.viewscontrols.focusgroup.TNBActivity;
import com.health.widget.EditTextWithDate;
import com.novabluetooth.views.twj.StringUtil;
import com.qmuiteam.qmui.util.QMUIDisplayHelper;
import com.qmuiteam.qmui.widget.QMUITabSegment;

import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TJFragment extends NovaFragment {
    private static final String TAG = "TJFragment";

    private View satisfactionView = null;
    private Map<ContentPage, View> mPageMap = new HashMap<>();
    List<JTYSDoctorEvaluateBean> list;
    SatisfactionAdapter adapter;
    JTYSHome home = null;
    private String myd;
    private int mCurrentItemCount = 10;
    private QMUITabSegment mTabSegment;
    private ContentPage mDestPage = ContentPage.Item1;
    private ViewPager mContentViewPager;
    private RecyclerView rvList;
    private int loadPage = 0;
    private EditTextWithDate ed_startdate;
    private EditTextWithDate ed_enddate;

    @Override
    public View findViews(LayoutInflater inflater) {
        satisfactionView = inflater.inflate(R.layout.fragment_tj, null);
        initViews(satisfactionView);
        initTabAndPager();
        return satisfactionView;
    }

    private void initViews(View view) {
        mTabSegment = (QMUITabSegment)view.findViewById(R.id.tabSegment);
        mContentViewPager = (ViewPager)view.findViewById(R.id.contentViewPager);
        LinearLayout ll_back = (LinearLayout)view.findViewById(R.id.ll_back);
        TextView tvJg = (TextView)view.findViewById(R.id.tv_jg);
        SharedPreferences shareDatas = getActivity().getSharedPreferences(SPUtil.SP_NAME, Context.MODE_PRIVATE);
        String jg = shareDatas.getString(SPUtil.STR_JDDW,"");
        tvJg.setText(jg);
        ll_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getActivity().finish();
            }
        });
        // list =
        // NovaApplication.fdbmanger.findAllByWhere(JTYSDoctorEvaluateBean.class,
        // "creater='" + NovaApplication.getJgrno() + "'");
        list = new ArrayList<>();

    }


    private PagerAdapter mPagerAdapter = new PagerAdapter() {
        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public int getCount() {
            return 4;
        }

        @RequiresApi(api = Build.VERSION_CODES.N)
        @Override
        public Object instantiateItem(final ViewGroup container, int position) {
            ContentPage page = ContentPage.getPage(position);
            View view = getPageView(page);
            view.setTag(page);
            ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            container.addView(view, params);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public int getItemPosition(@NonNull Object object) {
            View view = (View) object;
            Object page = view.getTag();
            if (page instanceof ContentPage) {
                int pos = ((ContentPage) page).getPosition();
                if (pos >= mCurrentItemCount) {
                    return POSITION_NONE;
                }
                return POSITION_UNCHANGED;
            }
            return POSITION_NONE;
        }
    };

    @SuppressLint("JavascriptInterface")
    @RequiresApi(api = Build.VERSION_CODES.N)
    private View getPageView(ContentPage page) {
//        View view = mPageMap.get(page);
        int position = page.getPosition();
        Log.e("TAG","当前位置："+position);

        View view = View.inflate(getActivity(), R.layout.fragment_zl, null);
        TextView tvDef = (TextView) view.findViewById(R.id.tv_def);
        TextView tvDef2 = (TextView) view.findViewById(R.id.tv_def2);
        TextView tvDef3 = (TextView) view.findViewById(R.id.tv_def3);
        TextView tvDef4 = (TextView) view.findViewById(R.id.tv_def4);
        TextView tvDef5 = (TextView) view.findViewById(R.id.tv_def5);
        TextView tvDef6 = (TextView) view.findViewById(R.id.tv_def6);
        TextView tvDef7 = (TextView) view.findViewById(R.id.tv_def7);
        TextView tvDef8 = (TextView) view.findViewById(R.id.tv_def8);

        PieChartView pieChartView = (PieChartView) view.findViewById(R.id.pie_chart);
        String str_jdrno = SPUtil.getStr_jdrno(getActivity());
        int count =NovaApplication.dbManger.getCountByJDR(str_jdrno);
        float man = jsbfb2(NovaApplication.dbManger.getCountByJDRSEX(str_jdrno, "DM01-08_1"), count);
        float woman = jsbfb2(NovaApplication.dbManger.getCountByJDRSEX(str_jdrno, "DM01-08_2"), count);

        PieChartView.PieItemBean[] items = new PieChartView.PieItemBean[]{
                new PieChartView.PieItemBean("男性", man),
                new PieChartView.PieItemBean("女性", woman)
        };
        pieChartView.setPieItems(items);
        if(position == 1){
            view = View.inflate(getActivity(), R.layout.fragment_sf, null);
            rvList = (RecyclerView) view.findViewById(R.id.rv_list);
            ed_startdate = (EditTextWithDate) view.findViewById(R.id.ed_startdate);
            ed_enddate = (EditTextWithDate) view.findViewById(R.id.ed_enddate);

            List<CD_DIABETESFOLLOWUP> list = NovaApplication.fdbmanger.findAllByWhere(CD_DIABETESFOLLOWUP.class," CREATER='"+str_jdrno+"'");
            List<SuiFangBean> list1 = new ArrayList<>();
            for (CD_DIABETESFOLLOWUP lists:list){
                SuiFangBean bean = new SuiFangBean();
                bean.setIDCARD(lists.getIDCARD());
                bean.setFOLLOWUP_DATE(lists.getFOLLOWUP_DATE());
                bean.setFOLLOWUPDOCTOR(lists.getFOLLOWUPDOCTOR());
                bean.setIS_UPDATE(lists.getIS_UPDATE());
                bean.setSfnum(lists.getRES3());
                bean.setTYPE("糖尿病");
                if(null!=lists.getRes16()){
                    if("NEXT_M_M_1".equals(lists.getRes16())){
                        bean.setXybglcs("常规随访");
                    }else if("NEXT_M_M_2".equals(lists.getRes16())){
                        bean.setXybglcs("第1次控制不满意2周随访");
                    }else if("NEXT_M_M_3".equals(lists.getRes16())){
                        bean.setXybglcs("两次控制不满意转诊随访");
                    }else if("NEXT_M_M_4".equals(lists.getRes16())){
                        bean.setXybglcs("紧急转诊");
                    }
                }
                if(null!=lists.getFOLLOWUPTYPE()){
                    if("DM01-71_1".equals(lists.getFOLLOWUPTYPE())){
                        bean.setCcsffl("控制满意");
                    }else if("DM01-71_2".equals(lists.getFOLLOWUPTYPE())){
                        bean.setCcsffl("控制不满意");
                    }else if("DM01-71_3".equals(lists.getFOLLOWUPTYPE())){
                        bean.setCcsffl("不良反应");
                    }else if("DM01-71_4".equals(lists.getFOLLOWUPTYPE())){
                        bean.setCcsffl("并发症");
                    }
                }
                list1.add(bean);
            }
            List<CD_HYPERTENSIONFOLLOWUP> list2 = NovaApplication.fdbmanger.findAllByWhere(CD_HYPERTENSIONFOLLOWUP.class, " CREATER='"+str_jdrno+"'");
            for (CD_HYPERTENSIONFOLLOWUP lists:list2){
                SuiFangBean bean = new SuiFangBean();
                bean.setIDCARD(lists.getIDCARD());
                bean.setFOLLOWUP_DATE(lists.getFOLLOWUPDATE());
                bean.setFOLLOWUPDOCTOR(lists.getFOLLOWUPDOCTOR());
                bean.setIS_UPDATE(lists.getIS_UPDATE());
                bean.setTYPE("高血压");
                bean.setSfnum(lists.getRES3());
                if(null!=lists.getRes16()){
                    if("NEXT_M_M_1".equals(lists.getRes16())){
                        bean.setXybglcs("常规随访");
                    }else if("NEXT_M_M_2".equals(lists.getRes16())){
                        bean.setXybglcs("第1次控制不满意2周随访");
                    }else if("NEXT_M_M_3".equals(lists.getRes16())){
                        bean.setXybglcs("两次控制不满意转诊随访");
                    }else if("NEXT_M_M_4".equals(lists.getRes16())){
                        bean.setXybglcs("紧急转诊");
                    }
                }
                if(null!=lists.getFOLLOWUPTYPE()){
                    if("DM01-71_1".equals(lists.getFOLLOWUPTYPE())){
                        bean.setCcsffl("控制满意");
                    }else if("DM01-71_2".equals(lists.getFOLLOWUPTYPE())){
                        bean.setCcsffl("控制不满意");
                    }else if("DM01-71_3".equals(lists.getFOLLOWUPTYPE())){
                        bean.setCcsffl("不良反应");
                    }else if("DM01-71_4".equals(lists.getFOLLOWUPTYPE())){
                        bean.setCcsffl("并发症");
                    }
                }
                list1.add(bean);
            }

            List<CD_MENTALDISEASE_FOLLOWUP> queses = NovaApplication.fdbmanger.findAllByWhere(CD_MENTALDISEASE_FOLLOWUP.class, " CREATER='"+str_jdrno+"'");
            for (CD_MENTALDISEASE_FOLLOWUP lists:queses){
                SuiFangBean bean = new SuiFangBean();
                bean.setIDCARD(lists.getIDCARD());
                bean.setFOLLOWUP_DATE(lists.getFOLLOWUP_DATE());
                bean.setFOLLOWUPDOCTOR(lists.getFOLLOWUPDOCTOR());
                bean.setIS_UPDATE(lists.getIS_UPDATE());
                bean.setTYPE("精神病");
                list1.add(bean);
            }

            Collections.sort(list1,new Comparator<SuiFangBean>(){
                @Override
                public int compare(SuiFangBean o1, SuiFangBean o2) {
                    return o2.getFOLLOWUP_DATE().compareTo(o1.getFOLLOWUP_DATE());
                }
            });

            for (SuiFangBean lists:list1){
                Log.e("TAG",GlobalConts.datetimeFormat.format(lists.getFOLLOWUP_DATE())+"-日期");
            }
            SuifangAdapter suifangAdapter = new SuifangAdapter(getContext(),list1,list1.size());

            rvList.setAdapter(suifangAdapter);
            GridLayoutManager manager = new GridLayoutManager(getContext(), 1);
            //设置布局管理者
            rvList.setLayoutManager(manager);


            ed_startdate.addTextChangedListener(new TextWatcher() {

                private Date date2;
                private Date date1;

                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    if(ed_enddate.getText().toString().trim().equals(ed_startdate.getText().toString())){
                        Toast.makeText(getActivity(), "请选择正确的开始日期", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    Log.e("TAG","结束："+ ed_enddate.getText());
                    if(!ed_enddate.getText().toString().equals("") && ed_enddate.getText().toString() != null){
                        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

                        try {
                            date1 = formatter.parse(ed_startdate.getText().toString().trim());
                            Calendar calendar = new GregorianCalendar();
                            calendar.setTime(date1);
                            calendar.add(calendar.DATE,-1); //把日期往后增加一天,整数  往后推,负数往前移动
                            date1 =calendar.getTime(); //这个时间就是日期往后推一天的结果
                        } catch (ParseException e) {

                        }

                        try {
                            date2 = formatter.parse(ed_enddate.getText().toString().trim());
                            Calendar calendar = new GregorianCalendar();
                            calendar.setTime(date2);
                            calendar.add(calendar.DATE,1); //把日期往后增加一天,整数  往后推,负数往前移动
                            date2 =calendar.getTime(); //这个时间就是日期往后推一天的结果
                            Log.e("TAG","日期值："+date2);

                        } catch (ParseException e) {
                            e.printStackTrace();
                        }

                        String str_jdrno = SPUtil.getStr_jdrno(getActivity());

                        List<CD_DIABETESFOLLOWUP> list = NovaApplication.fdbmanger.findAllByWhere(CD_DIABETESFOLLOWUP.class,
                                " FOLLOWUP_DATE between '"+GlobalConts.dateFormat.format(date1) +
                                        "' and '"+GlobalConts.dateFormat.format(date2) +
                                        "' and CREATER='"+str_jdrno+"'");
                        List<SuiFangBean> list1 = new ArrayList<>();
                        for (CD_DIABETESFOLLOWUP lists:list){
                            SuiFangBean bean = new SuiFangBean();
                            bean.setIDCARD(lists.getIDCARD());
                            bean.setFOLLOWUP_DATE(lists.getFOLLOWUP_DATE());
                            bean.setFOLLOWUPDOCTOR(lists.getFOLLOWUPDOCTOR());
                            bean.setTYPE("糖尿病");
                            list1.add(bean);
                        }
                        List<CD_HYPERTENSIONFOLLOWUP> list2 = NovaApplication.fdbmanger.findAllByWhere(CD_HYPERTENSIONFOLLOWUP.class,
                                " FOLLOWUPDATE between '"+GlobalConts.dateFormat.format(date1)
                                        +"' and '"+GlobalConts.dateFormat.format(date2)
                                        +"' and CREATER='"+str_jdrno+"'" );
                        for (CD_HYPERTENSIONFOLLOWUP lists:list2){
                            SuiFangBean bean = new SuiFangBean();
                            bean.setIDCARD(lists.getIDCARD());
                            bean.setFOLLOWUP_DATE(lists.getFOLLOWUPDATE());
                            bean.setFOLLOWUPDOCTOR(lists.getFOLLOWUPDOCTOR());
                            bean.setTYPE("高血压");
                            list1.add(bean);
                        }

                        List<CD_MENTALDISEASE_FOLLOWUP> queses = NovaApplication.fdbmanger.findAllByWhere(CD_MENTALDISEASE_FOLLOWUP.class,
                                " FOLLOWUP_DATE between '"+GlobalConts.dateFormat.format(date1)
                                        +"' and '"+GlobalConts.dateFormat.format(date2)
                                        +"' and CREATER='"+str_jdrno+"'");
                        for (CD_MENTALDISEASE_FOLLOWUP lists:queses){
                            SuiFangBean bean = new SuiFangBean();
                            bean.setIDCARD(lists.getIDCARD());
                            bean.setFOLLOWUP_DATE(lists.getFOLLOWUP_DATE());
                            bean.setFOLLOWUPDOCTOR(lists.getFOLLOWUPDOCTOR());
                            bean.setTYPE("精神病");
                            list1.add(bean);
                        }

                        Collections.sort(list1,new Comparator<SuiFangBean>(){
                            @Override
                            public int compare(SuiFangBean o1, SuiFangBean o2) {
                                return o2.getFOLLOWUP_DATE().compareTo(o1.getFOLLOWUP_DATE());
                            }
                        });

                        for (SuiFangBean lists:list1){
                            Log.e("TAG",GlobalConts.datetimeFormat.format(lists.getFOLLOWUP_DATE())+"-日期");
                        }
                        SuifangAdapter suifangAdapter = new SuifangAdapter(getContext(),list1,list1.size());
                        rvList.setAdapter(suifangAdapter);
                        GridLayoutManager manager = new GridLayoutManager(getContext(), 1);
                        //设置布局管理者
                        rvList.setLayoutManager(manager);


                        Log.e("TAG","查询了");
                    }
                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            });

            ed_enddate.addTextChangedListener(new TextWatcher() {

                private Date date2;
                private Date date1;

                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    if(ed_enddate.getText().toString().trim().equals(ed_startdate.getText().toString())){
                        Toast.makeText(getActivity(), "请选择正确的结束日期", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    Log.e("TAG","开始："+ ed_startdate.getText());
                    if(!ed_startdate.getText().toString().equals("") && ed_startdate.getText().toString() != null){
                        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

                        try {
                            date1 = formatter.parse(ed_startdate.getText().toString().trim());
                            Calendar calendar = new GregorianCalendar();
                            calendar.setTime(date1);
                            calendar.add(calendar.DATE,-1); //把日期往后增加一天,整数  往后推,负数往前移动
                            date1 =calendar.getTime(); //这个时间就是日期往后推一天的结果
                            Log.e("TAG","GIAO:"+GlobalConts.dateFormat.format(date1));
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }

                        try {
                            date2 = formatter.parse(ed_enddate.getText().toString().trim());
                            Calendar calendar = new GregorianCalendar();
                            calendar.setTime(date2);
                            calendar.add(calendar.DATE,1); //把日期往后增加一天,整数  往后推,负数往前移动
                            date2 =calendar.getTime(); //这个时间就是日期往后推一天的结果
                            Log.e("TAG","GIAO2:"+GlobalConts.dateFormat.format(date2));

                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                        String str_jdrno = SPUtil.getStr_jdrno(getActivity());
                        List<CD_DIABETESFOLLOWUP> list = NovaApplication.fdbmanger.findAllByWhere(CD_DIABETESFOLLOWUP.class," FOLLOWUP_DATE between '"+GlobalConts.dateFormat.format(date1)+"' and '"+GlobalConts.dateFormat.format(date2)+"' and CREATER='"+str_jdrno+"'");
                        List<SuiFangBean> list1 = new ArrayList<>();
                        for (CD_DIABETESFOLLOWUP lists:list){
                            SuiFangBean bean = new SuiFangBean();
                            bean.setIDCARD(lists.getIDCARD());
                            bean.setFOLLOWUP_DATE(lists.getFOLLOWUP_DATE());
                            bean.setFOLLOWUPDOCTOR(lists.getFOLLOWUPDOCTOR());
                            bean.setTYPE("糖尿病");
                            list1.add(bean);
                        }
                        List<CD_HYPERTENSIONFOLLOWUP> list2 = NovaApplication.fdbmanger.findAllByWhere(CD_HYPERTENSIONFOLLOWUP.class, " FOLLOWUPDATE between '"+GlobalConts.dateFormat.format(date1)+"' and '"+GlobalConts.dateFormat.format(date2)+"' and CREATER='"+str_jdrno+"'");
                        for (CD_HYPERTENSIONFOLLOWUP lists:list2){
                            SuiFangBean bean = new SuiFangBean();
                            bean.setIDCARD(lists.getIDCARD());
                            bean.setFOLLOWUP_DATE(lists.getFOLLOWUPDATE());
                            bean.setFOLLOWUPDOCTOR(lists.getFOLLOWUPDOCTOR());
                            bean.setTYPE("高血压");
                            list1.add(bean);
                        }

                        List<CD_MENTALDISEASE_FOLLOWUP> queses = NovaApplication.fdbmanger.findAllByWhere(CD_MENTALDISEASE_FOLLOWUP.class, " FOLLOWUP_DATE between '"+GlobalConts.dateFormat.format(date1)+"' and '"+GlobalConts.dateFormat.format(date2)+"' and CREATER='"+str_jdrno+"'");
                        for (CD_MENTALDISEASE_FOLLOWUP lists:queses){
                            SuiFangBean bean = new SuiFangBean();
                            bean.setIDCARD(lists.getIDCARD());
                            bean.setFOLLOWUP_DATE(lists.getFOLLOWUP_DATE());
                            bean.setFOLLOWUPDOCTOR(lists.getFOLLOWUPDOCTOR());
                            bean.setTYPE("精神病");
                            list1.add(bean);
                        }

                        Collections.sort(list1,new Comparator<SuiFangBean>(){
                            @Override
                            public int compare(SuiFangBean o1, SuiFangBean o2) {
                                return o2.getFOLLOWUP_DATE().compareTo(o1.getFOLLOWUP_DATE());
                            }
                        });

                        for (SuiFangBean lists:list1){
                            Log.e("TAG",GlobalConts.datetimeFormat.format(lists.getFOLLOWUP_DATE())+"-日期");
                        }
                        SuifangAdapter suifangAdapter = new SuifangAdapter(getContext(),list1,list1.size());
                        rvList.setAdapter(suifangAdapter);
                        GridLayoutManager manager = new GridLayoutManager(getContext(), 1);
                        //设置布局管理者
                        rvList.setLayoutManager(manager);
                        Log.e("TAG","查询了");
                    }

                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            });
        }
        else if(position == 2){
            view = View.inflate(getActivity(), R.layout.test_ac, null);
            WebView wv = (WebView) view.findViewById(R.id.wv);
            WebSettings settings = wv.getSettings();
            // 设置编码格式
            settings.setDefaultTextEncodingName("utf-8");
            // 支持js
            settings.setJavaScriptEnabled(true);//必须得设置不然js代码不能实现

            settings.setJavaScriptCanOpenWindowsAutomatically(true);//允许js弹出窗口
            wv.getSettings().setDomStorageEnabled(true);
//            wv.loadUrl("http://183.66.66.49:8081/pad/index.html?creater="+ NovaApplication.getJgrno());
            wv.loadUrl("http://txmp.guantangyiliao.com/pad/index.html?creater="+ NovaApplication.getJgrno());
            //系统默认会通过手机浏览器打开网页，为了能够直接通过WebView显示网页，则必须设置
            settings.setBuiltInZoomControls(true);
            settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
            settings.setUseWideViewPort(true);
            settings.setLoadWithOverviewMode(true);
            settings.setGeolocationEnabled(true);
            settings.setDomStorageEnabled(true);
            wv.requestFocus();
            wv.setWebChromeClient(new WebChromeClient());
            wv.setWebViewClient(new WebViewClient() {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    //使用WebView加载显示url
                    view.loadUrl(url);
                    //返回true
                    return true;
                }
            });

            wv.addJavascriptInterface(getActivity(),"android");
//            test("450121196408183946","2020-5-20",1);
        }
        if(count != 0) {
            String jsbfb = jsbfb(NovaApplication.dbManger.getCountByJDRType(str_jdrno, PeopleType.YBRQ), count);

            tvDef.setText(jsbfb);
            String jsbfb2 = jsbfb(NovaApplication.dbManger.getCountByJDRType(str_jdrno, PeopleType.GXY), count);
            tvDef2.setText(jsbfb2);
            String jsbfb3 = jsbfb(NovaApplication.dbManger.getCountByJDRType(str_jdrno, PeopleType.TNB), count);
            tvDef3.setText(jsbfb3);
            String jsbfb4 = jsbfb(NovaApplication.dbManger.getCountByJDRType(str_jdrno, PeopleType.OLD), count);
            tvDef4.setText(jsbfb4);
            String jsbfb6 = jsbfb(NovaApplication.dbManger.getCountByJDRType(str_jdrno, PeopleType.WOMAN), count);
            tvDef5.setText(jsbfb6);
            String jsbfb7 = jsbfb(NovaApplication.dbManger.getCountByJDRType(str_jdrno, PeopleType.JSB), count);
            tvDef6.setText(jsbfb7);
            String jsbfb8 = jsbfb(NovaApplication.dbManger.getCountByJDRType(str_jdrno, PeopleType.CHILD), count);
            tvDef7.setText(jsbfb8);
            String jsbfb9 = jsbfb(NovaApplication.dbManger.getCountByJDRType(str_jdrno, PeopleType.FJH), count);
            tvDef8.setText(jsbfb9);
        }
        mPageMap.put(page, view);
        return view;
    }

    @JavascriptInterface
    public void callAndroid(String idCard,String date, int type) {
        Log.e(TAG, "callAndroid: idCard" + idCard + " date:" + date + " type:" + type);
        if (!TextUtils.isEmpty(idCard) && !TextUtils.isEmpty(date)) {
            ARCHIVE_BASEINFO info = new ARCHIVE_BASEINFO();
            info.setIDCARD(idCard);
//            info.setCREATED_DATE(date);
            NovaApplication.setUserInfo(info);
            SPUtil.setstr_idCard(getActivity(), idCard);
            if (type == 1) {
                getActivity().startActivity(new Intent(getActivity(), GXYActivity.class));
            }else if (type == 2) {
                getActivity().startActivity(new Intent(getActivity(), TNBActivity.class));
            }else {
                Toast.makeText(getActivity(),"类型有误type应为1或2： " + type,Toast.LENGTH_SHORT).show();
            }
        }else {
            Toast.makeText(getActivity(),"身份证号为空",Toast.LENGTH_SHORT).show();
        }
    }

    private void test(String idCard,String date, int type) {
        if (!TextUtils.isEmpty(idCard) && !TextUtils.isEmpty(date)) {
            ARCHIVE_BASEINFO info = new ARCHIVE_BASEINFO();
            info.setIDCARD(idCard);
//            info.setCREATED_DATE(date);
            NovaApplication.setUserInfo(info);
            SPUtil.setstr_idCard(getActivity(), idCard);
            if (type == 1) {
                getActivity().startActivity(new Intent(getActivity(), GXYActivity.class));
            }else if (type == 2) {
                getActivity().startActivity(new Intent(getActivity(), TNBActivity.class));
            }else {
                Toast.makeText(getActivity(),"类型有误type应为1或2：  " + type,Toast.LENGTH_SHORT).show();
            }
        }else {
            Toast.makeText(getActivity(),"身份证号为空",Toast.LENGTH_SHORT).show();
        }
    }

    private String jsbfb(int a,int b){

        NumberFormat numberFormat = NumberFormat.getInstance();
        numberFormat.setMaximumFractionDigits(2);
        String result = numberFormat.format((float)a/(float)b*100);
        System.out.println("a和b的百分比为:" + result + "%");
        return result + "%";
    }

    private float jsbfb2(int a,int b){

        NumberFormat numberFormat = NumberFormat.getInstance();
        numberFormat.setMaximumFractionDigits(2);
        String result = numberFormat.format((float)a/(float)b*100);
        System.out.println("a和b的百分比为:" + result + "%");
        return Float.parseFloat(result);
    }

    private void initTabAndPager() {
        mContentViewPager.setAdapter(mPagerAdapter);
        mContentViewPager.setCurrentItem(mDestPage.getPosition(), false);
        mTabSegment.addTab(new QMUITabSegment.Tab("总览"));
//        mTabSegment.addTab(new QMUITabSegment.Tab("签约"));
        mTabSegment.addTab(new QMUITabSegment.Tab("随访"));
//        mTabSegment.addTab(new QMUITabSegment.Tab("医联互通"));
        mTabSegment.setDefaultNormalColor(Color.parseColor("#393D40"));
        mTabSegment.setDefaultSelectedColor(Color.parseColor("#19B495"));
//		mTabSegment.set

        int space = QMUIDisplayHelper.dp2px(getContext(), 16);
        mTabSegment.setHasIndicator(true);
        mTabSegment.setMode(QMUITabSegment.MODE_SCROLLABLE);
        mTabSegment.setItemSpaceInScrollMode(space);
        mTabSegment.setupWithViewPager(mContentViewPager, false);
        mTabSegment.setPadding(space, 0, space, 0);
        mTabSegment.addOnTabSelectedListener(new QMUITabSegment.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int index) {
            }

            @Override
            public void onTabUnselected(int index) {
            }

            @Override
            public void onTabReselected(int index) {
            }

            @Override
            public void onDoubleTap(int index) {
            }
        });

        Log.e(TAG, "initTabAndPager: "+mTabSegment.getChildCount() );
    }

    @Override
    public void setData() {

    }

    @Override
    public void onResume() {
        super.onResume();
        loadPage++;
        if(loadPage >= 2){
            reOnLoad();
        }
        Log.e("ttttt", "onResume()");
        Log.e("tag", "TJFragment onResume " + list.size());
    }

    private void reOnLoad(){
        try {
            String str_jdrno = SPUtil.getStr_jdrno(getActivity());
            List<CD_DIABETESFOLLOWUP> list = NovaApplication.fdbmanger.findAllByWhere(CD_DIABETESFOLLOWUP.class," CREATER='"+str_jdrno+"'");
            List<SuiFangBean> list1 = new ArrayList<>();
            for (CD_DIABETESFOLLOWUP lists:list){
                SuiFangBean bean = new SuiFangBean();
                bean.setIDCARD(lists.getIDCARD());
                bean.setFOLLOWUP_DATE(lists.getFOLLOWUP_DATE());
                bean.setFOLLOWUPDOCTOR(lists.getFOLLOWUPDOCTOR());
                bean.setIS_UPDATE(lists.getIS_UPDATE());
                bean.setTYPE("糖尿病");
                if(null!=lists.getRes16()){
                    if("NEXT_M_M_1".equals(lists.getRes16())){
                        bean.setXybglcs("常规随访");
                    }else if("NEXT_M_M_2".equals(lists.getRes16())){
                        bean.setXybglcs("第1次控制不满意2周随访");
                    }else if("NEXT_M_M_3".equals(lists.getRes16())){
                        bean.setXybglcs("两次控制不满意转诊随访");
                    }else if("NEXT_M_M_4".equals(lists.getRes16())){
                        bean.setXybglcs("紧急转诊");
                    }
                }
                if(null!=lists.getFOLLOWUPTYPE()){
                    if("DM01-71_1".equals(lists.getFOLLOWUPTYPE())){
                        bean.setCcsffl("控制满意");
                    }else if("DM01-71_2".equals(lists.getFOLLOWUPTYPE())){
                        bean.setCcsffl("控制不满意");
                    }else if("DM01-71_3".equals(lists.getFOLLOWUPTYPE())){
                        bean.setCcsffl("不良反应");
                    }else if("DM01-71_4".equals(lists.getFOLLOWUPTYPE())){
                        bean.setCcsffl("并发症");
                    }
                }
                bean.setSfnum(lists.getRES3());
                list1.add(bean);
            }
            List<CD_HYPERTENSIONFOLLOWUP> list2 = NovaApplication.fdbmanger.findAllByWhere(CD_HYPERTENSIONFOLLOWUP.class, " CREATER='"+str_jdrno+"'");
            for (CD_HYPERTENSIONFOLLOWUP lists:list2){
                SuiFangBean bean = new SuiFangBean();
                bean.setIDCARD(lists.getIDCARD());
                bean.setFOLLOWUP_DATE(lists.getFOLLOWUPDATE());
                bean.setFOLLOWUPDOCTOR(lists.getFOLLOWUPDOCTOR());
                bean.setIS_UPDATE(lists.getIS_UPDATE());
                bean.setTYPE("高血压");
                if(null!=lists.getRes16()){
                    if("NEXT_M_M_1".equals(lists.getRes16())){
                        bean.setXybglcs("常规随访");
                    }else if("NEXT_M_M_2".equals(lists.getRes16())){
                        bean.setXybglcs("第1次控制不满意2周随访");
                    }else if("NEXT_M_M_3".equals(lists.getRes16())){
                        bean.setXybglcs("两次控制不满意转诊随访");
                    }else if("NEXT_M_M_4".equals(lists.getRes16())){
                        bean.setXybglcs("紧急转诊");
                    }
                }
                if(null!=lists.getFOLLOWUPTYPE()){
                    if("DM01-71_1".equals(lists.getFOLLOWUPTYPE())){
                        bean.setCcsffl("控制满意");
                    }else if("DM01-71_2".equals(lists.getFOLLOWUPTYPE())){
                        bean.setCcsffl("控制不满意");
                    }else if("DM01-71_3".equals(lists.getFOLLOWUPTYPE())){
                        bean.setCcsffl("不良反应");
                    }else if("DM01-71_4".equals(lists.getFOLLOWUPTYPE())){
                        bean.setCcsffl("并发症");
                    }
                }
                bean.setSfnum(lists.getRES3());
                list1.add(bean);
            }

            List<CD_MENTALDISEASE_FOLLOWUP> queses = NovaApplication.fdbmanger.findAllByWhere(CD_MENTALDISEASE_FOLLOWUP.class, " CREATER='"+str_jdrno+"'");
            for (CD_MENTALDISEASE_FOLLOWUP lists:queses){
                SuiFangBean bean = new SuiFangBean();
                bean.setIDCARD(lists.getIDCARD());
                bean.setFOLLOWUP_DATE(lists.getFOLLOWUP_DATE());
                bean.setFOLLOWUPDOCTOR(lists.getFOLLOWUPDOCTOR());
                bean.setIS_UPDATE(lists.getIS_UPDATE());
                bean.setTYPE("精神病");
                list1.add(bean);
            }

            Collections.sort(list1,new Comparator<SuiFangBean>(){
                @Override
                public int compare(SuiFangBean o1, SuiFangBean o2) {
                    return o2.getFOLLOWUP_DATE().compareTo(o1.getFOLLOWUP_DATE());
                }
            });

            for (SuiFangBean lists:list1){
                Log.e("TAG",GlobalConts.datetimeFormat.format(lists.getFOLLOWUP_DATE())+"-日期");
            }
            SuifangAdapter suifangAdapter = new SuifangAdapter(getContext(),list1,list1.size());
            rvList.setAdapter(suifangAdapter);
            GridLayoutManager manager = new GridLayoutManager(getContext(), 1);
            //设置布局管理者
            rvList.setLayoutManager(manager);


            ed_startdate.addTextChangedListener(new TextWatcher() {

                private Date date2;
                private Date date1;

                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    if(ed_enddate.getText().toString().trim().equals(ed_startdate.getText().toString())){
                        Toast.makeText(getActivity(), "请选择正确的开始日期", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    Log.e("TAG","结束："+ ed_enddate.getText());
                    if(!ed_enddate.getText().toString().equals("") && ed_enddate.getText().toString() != null){
                        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

                        try {
                            date1 = formatter.parse(ed_startdate.getText().toString().trim());
                            Calendar calendar = new GregorianCalendar();
                            calendar.setTime(date1);
                            calendar.add(calendar.DATE,-1); //把日期往后增加一天,整数  往后推,负数往前移动
                            date1 =calendar.getTime(); //这个时间就是日期往后推一天的结果
                        } catch (ParseException e) {

                        }

                        try {
                            date2 = formatter.parse(ed_enddate.getText().toString().trim());
                            Calendar calendar = new GregorianCalendar();
                            calendar.setTime(date2);
                            calendar.add(calendar.DATE,1); //把日期往后增加一天,整数  往后推,负数往前移动
                            date2 =calendar.getTime(); //这个时间就是日期往后推一天的结果
                            Log.e("TAG","日期值："+date2);

                        } catch (ParseException e) {
                            e.printStackTrace();
                        }

                        String str_jdrno = SPUtil.getStr_jdrno(getActivity());

                        List<CD_DIABETESFOLLOWUP> list = NovaApplication.fdbmanger.findAllByWhere(CD_DIABETESFOLLOWUP.class," FOLLOWUP_DATE between '"+GlobalConts.dateFormat.format(date1)+"' and '"+GlobalConts.dateFormat.format(date2)+"' and CREATER='"+str_jdrno+"'");
                        List<SuiFangBean> list1 = new ArrayList<>();
                        for (CD_DIABETESFOLLOWUP lists:list){
                            SuiFangBean bean = new SuiFangBean();
                            bean.setIDCARD(lists.getIDCARD());
                            bean.setFOLLOWUP_DATE(lists.getFOLLOWUP_DATE());
                            bean.setFOLLOWUPDOCTOR(lists.getFOLLOWUPDOCTOR());
                            bean.setTYPE("糖尿病");
                            list1.add(bean);
                        }
                        List<CD_HYPERTENSIONFOLLOWUP> list2 = NovaApplication.fdbmanger.findAllByWhere(CD_HYPERTENSIONFOLLOWUP.class, " FOLLOWUPDATE between '"+GlobalConts.dateFormat.format(date1)+"' and '"+GlobalConts.dateFormat.format(date2)+"' and CREATER='"+str_jdrno+"'");
                        for (CD_HYPERTENSIONFOLLOWUP lists:list2){
                            SuiFangBean bean = new SuiFangBean();
                            bean.setIDCARD(lists.getIDCARD());
                            bean.setFOLLOWUP_DATE(lists.getFOLLOWUPDATE());
                            bean.setFOLLOWUPDOCTOR(lists.getFOLLOWUPDOCTOR());
                            bean.setTYPE("高血压");
                            list1.add(bean);
                        }

                        List<CD_MENTALDISEASE_FOLLOWUP> queses = NovaApplication.fdbmanger.findAllByWhere(CD_MENTALDISEASE_FOLLOWUP.class, " FOLLOWUP_DATE between '"+GlobalConts.dateFormat.format(date1)+"' and '"+GlobalConts.dateFormat.format(date2)+"' and CREATER='"+str_jdrno+"'");
                        for (CD_MENTALDISEASE_FOLLOWUP lists:queses){
                            SuiFangBean bean = new SuiFangBean();
                            bean.setIDCARD(lists.getIDCARD());
                            bean.setFOLLOWUP_DATE(lists.getFOLLOWUP_DATE());
                            bean.setFOLLOWUPDOCTOR(lists.getFOLLOWUPDOCTOR());
                            bean.setTYPE("精神病");
                            list1.add(bean);
                        }

                        Collections.sort(list1,new Comparator<SuiFangBean>(){
                            @Override
                            public int compare(SuiFangBean o1, SuiFangBean o2) {
                                return o2.getFOLLOWUP_DATE().compareTo(o1.getFOLLOWUP_DATE());
                            }
                        });

                        for (SuiFangBean lists:list1){
                            Log.e("TAG",GlobalConts.datetimeFormat.format(lists.getFOLLOWUP_DATE())+"-日期");
                        }
                        SuifangAdapter suifangAdapter = new SuifangAdapter(getContext(),list1,list1.size());
                        rvList.setAdapter(suifangAdapter);
                        GridLayoutManager manager = new GridLayoutManager(getContext(), 1);
                        //设置布局管理者
                        rvList.setLayoutManager(manager);
                        Log.e("TAG","查询了");
                    }
                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            });

            ed_enddate.addTextChangedListener(new TextWatcher() {

                private Date date2;
                private Date date1;

                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    if(ed_enddate.getText().toString().trim().equals(ed_startdate.getText().toString())){
                        Toast.makeText(getActivity(), "请选择正确的结束日期", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    Log.e("TAG","开始："+ ed_startdate.getText());
                    if(!ed_startdate.getText().toString().equals("") && ed_startdate.getText().toString() != null){
                        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

                        try {
                            date1 = formatter.parse(ed_startdate.getText().toString().trim());
                            Calendar calendar = new GregorianCalendar();
                            calendar.setTime(date1);
                            calendar.add(calendar.DATE,-1); //把日期往后增加一天,整数  往后推,负数往前移动
                            date1 =calendar.getTime(); //这个时间就是日期往后推一天的结果
                            Log.e("TAG","GIAO:"+GlobalConts.dateFormat.format(date1));
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }

                        try {
                            date2 = formatter.parse(ed_enddate.getText().toString().trim());
                            Calendar calendar = new GregorianCalendar();
                            calendar.setTime(date2);
                            calendar.add(calendar.DATE,1); //把日期往后增加一天,整数  往后推,负数往前移动
                            date2 =calendar.getTime(); //这个时间就是日期往后推一天的结果
                            Log.e("TAG","GIAO2:"+GlobalConts.dateFormat.format(date2));

                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                        String str_jdrno = SPUtil.getStr_jdrno(getActivity());
                        List<CD_DIABETESFOLLOWUP> list = NovaApplication.fdbmanger.findAllByWhere(CD_DIABETESFOLLOWUP.class," FOLLOWUP_DATE between '"+GlobalConts.dateFormat.format(date1)+"' and '"+GlobalConts.dateFormat.format(date2)+"' and CREATER='"+str_jdrno+"'");
                        List<SuiFangBean> list1 = new ArrayList<>();
                        for (CD_DIABETESFOLLOWUP lists:list){
                            SuiFangBean bean = new SuiFangBean();
                            bean.setIDCARD(lists.getIDCARD());
                            bean.setFOLLOWUP_DATE(lists.getFOLLOWUP_DATE());
                            bean.setFOLLOWUPDOCTOR(lists.getFOLLOWUPDOCTOR());
                            bean.setTYPE("糖尿病");
                            list1.add(bean);
                        }
                        List<CD_HYPERTENSIONFOLLOWUP> list2 = NovaApplication.fdbmanger.findAllByWhere(CD_HYPERTENSIONFOLLOWUP.class, " FOLLOWUPDATE between '"+GlobalConts.dateFormat.format(date1)+"' and '"+GlobalConts.dateFormat.format(date2)+"' and CREATER='"+str_jdrno+"'");
                        for (CD_HYPERTENSIONFOLLOWUP lists:list2){
                            SuiFangBean bean = new SuiFangBean();
                            bean.setIDCARD(lists.getIDCARD());
                            bean.setFOLLOWUP_DATE(lists.getFOLLOWUPDATE());
                            bean.setFOLLOWUPDOCTOR(lists.getFOLLOWUPDOCTOR());
                            bean.setTYPE("高血压");
                            list1.add(bean);
                        }

                        List<CD_MENTALDISEASE_FOLLOWUP> queses = NovaApplication.fdbmanger.findAllByWhere(CD_MENTALDISEASE_FOLLOWUP.class, " FOLLOWUP_DATE between '"+GlobalConts.dateFormat.format(date1)+"' and '"+GlobalConts.dateFormat.format(date2)+"' and CREATER='"+str_jdrno+"'");
                        for (CD_MENTALDISEASE_FOLLOWUP lists:queses){
                            SuiFangBean bean = new SuiFangBean();
                            bean.setIDCARD(lists.getIDCARD());
                            bean.setFOLLOWUP_DATE(lists.getFOLLOWUP_DATE());
                            bean.setFOLLOWUPDOCTOR(lists.getFOLLOWUPDOCTOR());
                            bean.setTYPE("精神病");
                            list1.add(bean);
                        }

                        Collections.sort(list1,new Comparator<SuiFangBean>(){
                            @Override
                            public int compare(SuiFangBean o1, SuiFangBean o2) {
                                return o2.getFOLLOWUP_DATE().compareTo(o1.getFOLLOWUP_DATE());
                            }
                        });

                        for (SuiFangBean lists:list1){
                            Log.e("TAG",GlobalConts.datetimeFormat.format(lists.getFOLLOWUP_DATE())+"-日期");
                        }
                        SuifangAdapter suifangAdapter = new SuifangAdapter(getContext(),list1,list1.size());
                        rvList.setAdapter(suifangAdapter);
                        GridLayoutManager manager = new GridLayoutManager(getContext(), 1);
                        //设置布局管理者
                        rvList.setLayoutManager(manager);
                        Log.e("TAG","查询了");
                    }

                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void setupView(View view) {

    }

    @Override
    public void addListener() {

    }

    public enum ContentPage {
        Item1(0),
        Item2(1),
        Item3(2),
        Item4(3),
        Item5(4),
        Item6(5),
        Item7(6),
        Item8(7),
        Item9(8),
        Item10(9);
        private final int position;

        ContentPage(int pos) {
            position = pos;
        }

        public static ContentPage getPage(int position) {
            switch (position) {
                case 0:
                    return Item1;
                case 1:
                    return Item2;
                case 2:
                    return Item3;
                case 3:
                    return Item4;
                case 4:
                    return Item5;
                case 5:
                    return Item6;
                case 6:
                    return Item7;
                case 7:
                    return Item8;
                case 8:
                    return Item9;
                case 9:
                    return Item10;
                default:
                    return Item1;
            }
        }

        public int getPosition() {
            return position;
        }
    }
}
